package com.ItemsRent.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.struts2.ServletActionContext;


public class PictureAction {

	private static final int BUFFER_SIZE = 1024*40; 
	
	private List<File> upload;			  //封装上传文件域属性，多个文件的话可定义成File[]数组，反正保证与前端的变量名一样就行
	private List<String> uploadContentType; //封装上传文件类型
	private List<String> uploadFileName;    //封装上传文件名
	private String savePath;          //封装上传文件的保存路径              
	//private List<HashMap<String,String>> result = new ArrayList<HashMap<String,String>>();
	private HashMap<String, Object> result = new HashMap<String, Object>();
	

	public HashMap<String, Object> getResult() {
		return result;
	}
	public void setResult(HashMap<String, Object> result) {
		this.result = result;
	}
	public static int getBufferSize() {
		return BUFFER_SIZE;
	}
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public List<File> getUpload() {
		return upload;
	}
	public void setUpload(List<File> upload) {
		this.upload = upload;
	}
	public List<String> getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public List<String> getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	/**
	 * 将源文件复制成目标文件
	 * @param source  源文件
	 * @param target  目标文件
	 */
	private static void copy(File source, File target){ 
		InputStream inputStream = null; //声明一个输入流
		OutputStream outputStream = null;  //声明一个输出流
		try{
			inputStream = new BufferedInputStream(new FileInputStream(source), BUFFER_SIZE); //实例化输入流
			outputStream = new BufferedOutputStream(new FileOutputStream(target), BUFFER_SIZE); //实例化输出流
			byte[] buffer = new byte[BUFFER_SIZE]; //定义字节数组
			int length = 0; //定义临时参数对象
			while((length = inputStream.read(buffer))>0){  //上传文件字节数大于0
				outputStream.write(buffer, 0, length); //将内容以字节形式写入
			}
		}catch (Exception e) { 
			e.printStackTrace(); 
			System.out.println(e.getMessage()); 
		}finally{
			if(inputStream!=null){ 
				try{ 
					inputStream.close();
				}catch(Exception e2){ 
					e2.printStackTrace(); 
					System.out.println(e2.getMessage());
				} 
			} 
			if (outputStream!=null){
				try{
					outputStream.close();
				}catch(Exception e2){
					e2.printStackTrace();
					System.out.println(e2.getMessage());
				}
			}
		}
	}
	/**
	 * 
	 */
	public String execute() throws Exception {
		//System.out.println("this is ItemUploadAction");
		for(int i = 1; i<=upload.size(); i++){
		String path = ServletActionContext.getServletContext().getRealPath(this.getSavePath())+ "\\"+ this.uploadFileName.get(i-1);//文件保存路径
		String path2 ="upload" + "/"+ this.uploadFileName.get(i-1);
		//System.out.print("第"+i+"张:");
		//System.out.println(path);
		ServletActionContext.getRequest().getSession().setAttribute("picturePath", path2);
		File target = new File(path);
		if(!target.getParentFile().exists()){
			target.getParentFile().mkdirs();
		}
		copy(this.upload.get(i-1), target);
		result.put("path", path2);
		}
		result.put("code", "003");
		result.put("msg", "图片上传成功");
		//System.out.println("成功执行execute方法");
		return "success";
	}
}
